package Demo;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class TestCanvas extends Applet implements MouseListener, 
MouseMotionListener {
	
	private int x = 10, y = 10, w = 40, h = 40;
	private boolean isInside = false;
	private boolean isPressed = false;
	
	
	public void init () 
	{
		setSize (400, 400);
		addMouseListener (this);
		addMouseMotionListener (this);
		setBackground (Color.white);
		repaint ();
	}
	
	public void paint (Graphics g) {
		if (isInside) {
			if (isPressed) {
				g.setColor (Color.magenta);
				g.drawString ("Hey, I'm moving to (" + x + ", " + y + ")", x, y + h + 15);
				g.setColor (Color.red);
				g.fillRect (x, y, w, h);
			}
			else
			{
				g.setColor (Color.red);
				g.drawString ("Great! Now DRAG the box to move it!", x, y + h + 15);
				g.setColor (Color.blue);
				g.fillRect (x, y, w, h);
			}
		
		}
		else
		{
			g.setColor (Color.black);
			g.drawString ("Move the mouse inside the box!", x, y + h + 15);
			g.setColor (Color.gray);
			g.fillRect (x, y, w, h);
		}
	}
	
	
	@Override
	public void mouseDragged(MouseEvent e) {
		x = e.getX ();
		y = e.getY ();
		repaint ();
	}
	
	@Override
	public void mouseMoved(MouseEvent e) {
	
		int mx = e.getX ();
		int my = e.getY ();
		if (mx > x && mx < x + w && my > y && my < y + h) {
			isInside = true;
			repaint ();
		}
		else{
			if (isInside) {
				isInside = false;
				repaint ();
			}
			isInside = false;
		}
		
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void mousePressed(MouseEvent arg0) {
		isPressed = true;
	}
	
	@Override
	public void mouseReleased(MouseEvent arg0) {
		
		isPressed = false;
	}

}